﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MuaBanLike.com
{
    /// <summary>
    /// Interaction logic for PopupWindow.xaml
    /// </summary>
    public partial class PopupWindow : Window
    {

        public MainWindow mainWnd = null;
        
        public PopupWindow()
        {
            InitializeComponent();         
            m_bLike = false;
            m_bClose = false;           

        }


     
        public bool m_bLike;
        public bool m_bClose;
       
        public string status;

        bool loginFaceBook()
        {
            var doc = popupBrowser.Document as mshtml.HTMLDocument;
            if (doc != null)
            {
                var coll = doc.getElementsByTagName("input");

                mshtml.HTMLInputElement itemLogin = null;
                int nAddtion = 0;
                foreach (mshtml.HTMLInputElement item in coll)
                {
                    if (item != null)
                    {
                        if (item.type == "text" && item.name == "email")
                        {
                            item.focus();
                            item.value = "thelightsky@gmail.com";
                            nAddtion++;
                        }
                        else if (item.type == "password" && item.name == "pass")
                        {
                            item.focus();
                            item.value = "trinhminh";
                            nAddtion++;
                        }
                        else if (item.type == "submit" && item.value == "Log In")
                        {
                            itemLogin = item;
                            nAddtion++;
                        }
                        if (nAddtion == 3)
                            break;
                    }
                }

                if (nAddtion == 0)
                    return false;

                if (itemLogin != null)
                {
                    itemLogin.click();
                }
            }
            return true;
        }

        public void disposeWebControl()
        {
            this.popupBrowser.Dispose();
            tbLog.Text += "disposeWebControl";
        }

        DateTime startTime;
        void delaySomeMinute()
        {
            startTime = DateTime.Now;
            BackgroundWorker thread = new BackgroundWorker();

            thread.DoWork += delay5second;
            thread.RunWorkerCompleted += closePopup;
            thread.WorkerReportsProgress = true;
            thread.WorkerSupportsCancellation = true;
            thread.RunWorkerAsync();

        }


        private void closePopup(Object sender, RunWorkerCompletedEventArgs e)
        {
            this.popupBrowser.Dispose();

            if (mainWnd != null)
                mainWnd.onClosePopup();
        }

        private void delay5second(Object sender, DoWorkEventArgs e)
        {
            while (true)
            {

                TimeSpan time = DateTime.Now - startTime;

                if (time.Seconds > 5)
                {
                    break;
                }

            }
        }

        private void onLoadComplete(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            if (e.Uri.ToString() != "about:blank")
            {
                startDetect();
            }
        }

        private void onClosing(object sender, EventArgs e)
        {
            
        }


        private void delay1second(Object sender, DoWorkEventArgs e)
        {
            while (true)
            {

                TimeSpan time = DateTime.Now - startTime;

                if (time.Seconds > 1)
                {
                    break;
                }

            }
        }


        private void onDetect(Object sender, RunWorkerCompletedEventArgs e)
        {
            
                if (!loginFaceBook())
                {
                    var doc = (mshtml.HTMLDocument)popupBrowser.Document;

                    //u_0_1t (liked) , u_0_1u (like)
                    var lableTag = doc.getElementById("timelineHeadlineLikeButton");


                    if (lableTag != null)
                    {
                        foreach (var linkLike in lableTag.children)
                        {
                            mshtml.HTMLInputElement likedBtn = linkLike as mshtml.HTMLInputElement;
                            if (likedBtn != null)
                            {
                                tbLog.Text += "->Found like button";
                                likedBtn.click();
                                delaySomeMinute();
                            }
                        }
                        

                    }
                    else                      
                    {
                         var likedBtn = doc.getElementById("u_0_1t");
                         if (likedBtn != null)
                         {
                            tbLog.Text += "->Found Unlike button";
                            this.popupBrowser.Dispose();

                            if (mainWnd != null)
                                mainWnd.onClosePopup();
                         }
                         else
                         {
                             TimeSpan time = DateTime.Now - startTime;

                             if (time.Seconds < 6)
                             {
                                 tbLog.Text += "->Replay detect";
                                 startDetect();
                             }
                             else
                             {
                                 tbLog.Text += "->Stop detect";
                                 this.popupBrowser.Dispose();

                                 if (mainWnd != null)
                                     mainWnd.onClosePopup();

                                 m_bLike = false;
                                 m_bClose = true;
                             }
                         }
                    }
                   


                }
            
        }

        void startDetect()
        {
            startTime = DateTime.Now;
            BackgroundWorker thread = new BackgroundWorker();

            thread.DoWork += delay1second;
            thread.RunWorkerCompleted += onDetect;
            thread.WorkerReportsProgress = true;
            thread.WorkerSupportsCancellation = true;
            thread.RunWorkerAsync();

        }

        private void OnNavigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            tbLog.Text += "->Navigated(" + e.Uri.ToString() + ")";
            // start to detect like

            
        }

        private void OnNavigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
        {            
            tbLog.Text += "->Navigating(" + e.Uri.ToString() + ")";
        }
    }
}
